$(function() {

	// ***********************************parameters***********************************
	// ********************************************************************************

	var tail = 0;
	var body = 0;
	var head = 0;
	var pos = 0;

	var myRadioValue;
	var $write = $('#write'), shift = false, capslock = false;
	var lead = [ [ '1', 'G', 'ᄀ', 'ㄱ', "&#x1100" ],
			[ '2', 'GG', 'ᄁ', 'ㄲ', "&#x1101" ],
			[ '3', 'N', 'ᄂ', 'ㄴ', "&#x1102" ],
			[ '4', 'D', 'ᄃ', 'ㄷ', "&#x1103" ],
			[ '5', 'DD', 'ᄄ', 'ㄸ', "&#x1104" ],
			[ '6', 'R', 'ᄅ', 'ㄹ', "&#x1105" ],
			[ '7', 'M', 'ᄆ', 'ㅁ', "&#x1106" ],
			[ '8', 'B', 'ᄇ', 'ㅂ', "&#x1107" ],
			[ '9', 'BB', 'ᄈ', 'ㅃ', "&#x1108" ],
			[ '10', 'S', 'ᄉ', 'ㅅ', "&#x1109" ],
			[ '11', 'SS', 'ᄊ', 'ㅆ', "&#x110A" ],
			[ '12', '', 'ᄋ', 'ㅇ', "&#x110B" ],
			[ '13', 'J', 'ᄌ', 'ㅈ', "&#x110C" ],
			[ '14', 'JJ', 'ᄍ', 'ㅉ', "&#x110D" ],
			[ '15', 'C', 'ᄎ', 'ㅊ', "&#x110E" ],
			[ '16', 'K', 'ᄏ', 'ㅋ', "&#x110F" ],
			[ '17', 'T', 'ᄐ', 'ㅌ', "&#x1110" ],
			[ '18', 'P', 'ᄑ', 'ㅍ', "&#x1111" ],
			[ '19', 'H', 'ᄒ', 'ㅎ', "&#x1112" ] ];

	var voyel = [ [ '1', 'A', 'ᅡ', 'ㅏ', "&#x1161" ],
			[ '2', 'AE', 'ᅢ', 'ㅐ', "&#x1162" ],
			[ '3', 'YA', 'ᅣ', 'ㅑ', "&#x1163" ],
			[ '4', 'YAE', 'ᅤ', 'ㅒ', "&#x1164" ],
			[ '5', 'EO', 'ᅥ', 'ㅓ', "&#x1165" ],
			[ '6', 'E', 'ᅦ', 'ㅔ', "&#x1166" ],
			[ '7', 'YEO', 'ᅧ', 'ㅕ', "&#x1167" ],
			[ '8', 'YE', 'ᅨ', 'ㅖ', "&#x1168" ],
			[ '9', 'O', 'ᅩ', 'ㅗ', "&#x1169" ],
			[ '10', 'WA', 'ᅪ', 'ㅘ', "&#x116A" ],
			[ '11', 'WAE', 'ᅫ', 'ㅙ', "&#x116B" ],
			[ '12', 'OE', 'ᅬ', 'ㅚ', "&#x116C" ],
			[ '13', 'YO', 'ᅭ', 'ㅛ', "&#x116D" ],
			[ '14', 'U', 'ᅮ', 'ㅜ', "&#x116E" ],
			[ '15', 'WEO', 'ᅯ', 'ㅝ', "&#x116F" ],
			[ '16', 'WE', 'ᅰ', 'ㅞ', "&#x1170" ],
			[ '17', 'WI', 'ᅱ', 'ㅟ', "&#x1171" ],
			[ '18', 'YU', 'ᅲ', 'ㅠ', "&#x1172" ],
			[ '19', 'EU', 'ᅳ', 'ㅡ', "&#x1173" ],
			[ '20', 'YI', 'ᅴ', 'ㅢ', "&#x1174" ],
			[ '21', 'I', 'ᅵ', 'ㅣ', "&#x1175" ], ];

	var consonant = [ [ '1', 'G', 'ᆨ', 'ㄱ', "&#x11A8" ],
			[ '2', 'GG', 'ᆩ', 'ㄲ', "&#x11A9" ],
			[ '3', 'GS', 'ᆪ', 'ㄳ', "&#x11AA" ],
			[ '4', 'N', 'ᆫ', 'ㄴ', "&#x11AB" ],
			[ '5', 'NJ', 'ᆬ', 'ㄵ', "&#x11AC" ],
			[ '6', 'NH', 'ᆭ', 'ㄶ', "&#x11AD" ],
			[ '7', 'D', 'ᆮ', 'ㄷ', "&#x11AE" ],
			[ '8', 'L', 'ᆯ', 'ㄹ', "&#x11AF" ],
			[ '9', 'LG', 'ᆰ', 'ㄺ', "&#x11B0" ],
			[ '10', 'LM', 'ᆱ', 'ㄻ', "&#x11B1" ],
			[ '11', 'LB', 'ᆲ', 'ㄼ', "&#x11B2" ],
			[ '12', 'LS', 'ᆳ', 'ㄽ', "&#x11B3" ],
			[ '13', 'LT', 'ᆴ', 'ㄾ', "&#x11B4" ],
			[ '14', 'LP', 'ᆵ', 'ㄿ', "&#x11B5" ],
			[ '15', 'LH', 'ᆶ', 'ㅀ', "&#x11B6" ],
			[ '16', 'M', 'ᆷ', 'ㅁ', "&#x11B7" ],
			[ '17', 'B', 'ᆸ', 'ㅂ', "&#x11B8" ],
			[ '18', 'BS', 'ᆹ', 'ㅄ', "&#x11B9" ],
			[ '19', 'S', 'ᆺ', 'ㅅ', "&#x11BA" ],
			[ '20', 'SS', 'ᆻ', 'ㅆ', "&#x11BB" ],
			[ '21', 'NG', 'ᆼ', 'ㅇ', "&#x11BC" ],
			[ '22', 'J', 'ᆽ', 'ㅈ', "&#x11BD" ],
			[ '23', 'C', 'ᆾ', 'ㅊ', "&#x11BE" ],
			[ '24', 'K', 'ᆿ', 'ㅋ', "&#x11BF" ],
			[ '25', 'T', 'ᇀ', 'ㅌ', "&#x11C0" ],
			[ '26', 'P', 'ᇁ', 'ㅍ', "&#x11C1" ],
			[ '27', 'H', 'ᇂ', 'ㅎ', "&#x11C2" ], ];

	// ***********************************functions***********************************
	// *******************************************************************************

	function appendLetter(letter) {
		$("#keyboard ul").append('<li class="letter">' + letter + '</li>');
	}

	function appendSymbol(lower, upper, value) {
		$("#keyboard ul")
				.append(
						'<li class="symbol", value="'
								+ value
								+ '" onmouseover="this.style.backgroundColor=\'burlywood\'"onmouseout="this.style.backgroundColor=\'white\'"><span class="off">'
								+ lower + '</span><span class="on">' + upper
								+ '</span></li>');
	}

	function appendSpecial(specialclass, specialname) {
		$("#keyboard ul").append(
				'<li class="' + specialclass + '">' + specialname + '</li>');
	}

	function radioValue(jqRadioButton) {
		if (jqRadioButton.length) {
			myRadioValue = jqRadioButton.val();
		} else {
			myRadioValue = 0;
		}
	}

	function letter(type, i) {
		if (type == "1") {
			return lead[i - 1][3];
		}
		if (type == "2") {
			return voyel[i - 1][3];
		}
		if (type == "3") {
			return consonant[i - 1][3];
		}
	}

	function getValueHead(letter) {
		for ( var i = 0; i < lead.length; i++) {
			if (lead[i][3] == letter) {
				return lead[i][0];
			}
		}
		return 0;
	}
	function getValueBody(letter) {
		for ( var i = 0; i < voyel.length; i++) {
			if (voyel[i][3] == letter) {
				return voyel[i][0];
			}
		}
		return 0;
	}
	function getValueTail(letter) {
		for ( var i = 0; i < consonant.length; i++) {
			if (consonant[i][3] == letter) {
				return consonant[i][0];
			}
		}
		return 0;
	}

	function determineHangul() {
		if (!tail) {
			tail = 0;
		}
		if (!body) {
			body = 0;
		}
		if (!head) {
			head = 0;
		}
		var v = 44032;
		tail = parseInt(tail, 10);
		body = parseInt(body, 10);
		head = parseInt(head, 10);
		if (head > 0) {
			v = v + ((head - 1) * 588);
		}
		if (body > 0) {
			v = v + ((body - 1) * 28);
			// alert(tail+ " "+body+" "+head + " "+v);
		} else {
			return 0;
		}
		if (tail > 0) {
			v = v + tail;
		} else {
			return 0;
		}
		return v;
	}

	function createKoreanKeyboard() {
		$("#keyboard ul").empty();
		appendSymbol('`', '~');
		appendSymbol('1', '!');
		appendSymbol('2', '@');
		appendSymbol('3', '#');
		appendSymbol('4', '$');
		appendSymbol('5', '%');
		appendSymbol('6', '^');
		appendSymbol('7', '&');
		appendSymbol('8', '*');
		appendSymbol('9', '(');
		appendSymbol('0', ')');
		appendSymbol('-', '_');
		appendSymbol('=', '+');
		appendSpecial("delete lastitem", "delete");
		appendSpecial("tab", "tab");
		appendSymbol(letter(1, 8), letter(1, 9));
		appendSymbol(letter(1, 13), letter(1, 14));
		appendSymbol(letter(1, 4), letter(1, 5));
		appendSymbol(letter(1, 1), letter(1, 2));
		appendSymbol(letter(1, 10), letter(1, 11));
		appendSymbol(letter(2, 13), letter(2, 13));
		appendSymbol(letter(2, 7), letter(2, 7));
		appendSymbol(letter(2, 3), letter(2, 3));
		appendSymbol(letter(2, 2), letter(2, 4));
		appendSymbol(letter(2, 3), letter(2, 8));
		appendSymbol('[', '{');
		appendSymbol(']', '}');
		appendSpecial("return lastitem", "return");
		appendSpecial("capslock", "caps lock");
		appendSymbol(letter(1, 7), letter(1, 7));
		appendSymbol(letter(1, 3), letter(1, 3));
		appendSymbol(letter(1, 12), letter(1, 12));
		appendSymbol(letter(1, 6), letter(1, 6));
		appendSymbol(letter(1, 19), letter(1, 19));
		appendSymbol(letter(2, 9), letter(2, 9));
		appendSymbol(letter(2, 5), letter(2, 5));
		appendSymbol(letter(2, 1), letter(2, 1));
		appendSymbol(letter(2, 21), letter(2, 21));
		appendSymbol(';', ':');
		appendSymbol("'", '"');
		appendSymbol('\\', '|');
		appendSpecial("left-shift", "shift");
		appendSymbol('\\', '|');
		appendSymbol(letter(1, 16), letter(1, 16));
		appendSymbol(letter(1, 17), letter(1, 17));
		appendSymbol(letter(1, 15), letter(1, 15));
		appendSymbol(letter(1, 18), letter(1, 18));
		appendSymbol(letter(2, 18), letter(2, 18));
		appendSymbol(letter(2, 14), letter(2, 14));
		appendSymbol(letter(2, 19), letter(2, 19));
		appendSymbol(',', '<');
		appendSymbol('.', '>');
		appendSymbol('/', '?');
		appendSpecial("right-shift lastitem", "shift");
		appendSpecial("space lastitem", "&nbsp");
	}
	function createFrenchKeyboard() {
		$("#keyboard ul").empty();
		appendSymbol('²', '');
		appendSymbol('&', '1');
		appendSymbol('é', '2');
		appendSymbol('"', '3');
		appendSymbol("'", '4');
		appendSymbol('(', '5');
		appendSymbol('-', '6');
		appendSymbol('è', '7');
		appendSymbol('_', '8');
		appendSymbol('ç', '9');
		appendSymbol('à', '0');
		appendSymbol(')', '°');
		appendSymbol('=', '+');
		appendSpecial("delete lastitem", "delete");
		appendSpecial("tab", "tab");
		appendSymbol('a', 'A');
		appendSymbol('z', 'Z');
		appendSymbol('e', 'E');
		appendSymbol('r', 'R');
		appendSymbol('t', 'T');
		appendSymbol('y', 'Y');
		appendSymbol('u', 'U');
		appendSymbol('i', 'I');
		appendSymbol('o', 'O');
		appendSymbol('p', 'P');
		appendSymbol('^', '¨');
		appendSymbol('$', '£');
		appendSpecial("return lastitem", "return");
		appendSpecial("capslock", "caps lock");
		appendSymbol('q', 'Q');
		appendSymbol('s', 'S');
		appendSymbol('d', 'D');
		appendSymbol('f', 'F');
		appendSymbol('g', 'G');
		appendSymbol('h', 'H');
		appendSymbol('j', 'J');
		appendSymbol('k', 'K');
		appendSymbol('l', 'L');
		appendSymbol('m', 'M');
		appendSymbol('ù', '%');
		appendSymbol('*', 'µ');
		appendSpecial("left-shift", "shift");
		appendSymbol('<', '<');
		appendSymbol('w', 'W');
		appendSymbol('x', 'X');
		appendSymbol('c', 'C');
		appendSymbol('v', 'V');
		appendSymbol('b', 'B');
		appendSymbol('n', 'N');
		appendSymbol(',', '?');
		appendSymbol(';', '.');
		appendSymbol(':', '/');
		appendSymbol('!', '§');
		appendSpecial("right-shift lastitem", "shift");
		appendSpecial("space lastitem", "&nbsp");
	}

	function createKeyboard() {
		radioValue($('input[name=langGroup]:radio:checked'));
		if (myRadioValue == 'Korean') {
			createKoreanKeyboard();
		}
		if (myRadioValue == 'French') {
			createFrenchKeyboard();
		}
	}

	function checkKeycode(e) {
		var keycode;
		if (window.event) {
			keycode = window.event.keyCode;
		} else if (e)
			keycode = e.which;

		var character = String.fromCharCode(keycode);
		return keycode;
	}

	// ***********************************listeners***********************************
	// *******************************************************************************

	function addKeyboardListeners() {
		 document.onkeypress = checkKeycode;
			var letterDiv = document.getElementById("write");
			letterDiv.innerHTML = keycode;
			
//		$('#keyboard li').click(function() {
//			var $this = $(this), character = $this.html();
//			// If it's a lowercase letter, nothing happens to this variable
//			// Shift keys
//			if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
//				$('.letter').toggleClass('uppercase');
//				$('.symbol span').toggle();
//
//				shift = (shift === true) ? false : true;
//				capslock = false;
//				return false;
//			}
//
//			// Caps lock
//			if ($this.hasClass('capslock')) {
//				$('.letter').toggleClass('uppercase');
//				capslock = true;
//				return false;
//			}
//
//			// Delete
//			if ($this.hasClass('delete')) {
//				var html = $write.html();
//
//				$write.html(html.substr(0, html.length - 1));
//				return false;
//			}
//
//			// Special characters
//			if ($this.hasClass('symbol'))
//				character = $('span:visible', $this).html();
//			if ($this.hasClass('space'))
//				character = ' ';
//			if ($this.hasClass('tab'))
//				character = "\t";
//			if ($this.hasClass('return'))
//				character = "\n";
//
//			// Uppercase letter
//			if ($this.hasClass('uppercase'))
//				character = character.toUpperCase();
//
//			// Remove shift once a key is clicked.
//			if (shift === true) {
//				$('.symbol span').toggle();
//				if (capslock === false)
//					$('.letter').toggleClass('uppercase');
//
//				shift = false;
//			}
//			if (myRadioValue == 'Korean') {
//				// Add the character
//				var m = pos % 3;
//				switch (m) {
//				case 0:
//					var v = getValueHead(character);
//					if (v == 0) {
//						pos--;
//					}
//					head = v;
//					body = 0;
//					tail = 0;
//					break;
//				case 1:
//					var v = getValueBody(character);
//					if (v == 0) {
//						pos--;
//					}
//					body = v;
//					tail = 0;
//					break;
//				case 2:
//					var v = getValueTail(character);
//					if (v == 0) {
//						pos--;
//					}
//					tail = v;
//					break;
//				}
//				pos++;
//
//				var hangul = determineHangul();
//				if (hangul != 0) {
//					// $write.val("&#" + hangul);
//					// txtArea.value = "&#" + hangul;
//					$write.html($write.html() + "&#" + hangul);
//				}
//			} else {
//				$write.html($write.html() + character);
//			}
//		});
	}
	$('input[name=langGroup]:radio').change(function() {
		radioValue($('input[name=langGroup]:radio:checked'));
		createKeyboard();
		addKeyboardListeners();
	});

	// ***********************************run***********************************
	// *************************************************************************

	$(createKeyboard);
	$(addKeyboardListeners);
});
